package main

import (
	"bytes"
	"html/template"
	"log"

	"github.com/savsgio/atreugo/v11"
)

type Data struct {
	Username string
}

func main() {
	config := atreugo.Config{
		Addr: "0.0.0.0:8001",
	}
	server := atreugo.New(config)

	server.GET("/", func(ctx *atreugo.RequestCtx) error {
		t, err := template.ParseFiles("./template/index.html")
		if err != nil {
			log.Fatal("Parse file template error:", err)
		}
		data := Data{Username: string(ctx.FormValue("username"))}
		buf := new(bytes.Buffer)
		err = t.Execute(buf, data)
		if err != nil {
			log.Fatal("Execute file template error:", err)
		}

		return ctx.HTTPResponse(buf.String())
	})

	if err := server.ListenAndServe(); err != nil {
		panic(err)
	}
}
